var createError = require('http-errors');
var express = require('express');

var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
//引入管理员注册登录路由
var adminsRouter = require('./routes/admins');
//引入用户注册登录路由
var usersRouter = require('./routes/users');
// 引入令牌验证
const { jwtAuth } = require("tools-factory-tang/jwt");
// 引入电影路由
var moviesRouter = require('./routes/movies');
// 引入初始化路由
var indexRouter = require('./routes/index');
// 引入初始化路由
var schedulesRouter = require('./routes/schedules');
// 引入放映厅路由接口
var projectionHallRouter = require('./routes/projectionHall');
// 引入电影路由
var moviesRouter = require('./routes/movies');
// 引入影院路由
var cinemasRouter = require("./routes/cinemas");
// 引入座位路由
var seatRouter = require("./routes/seat");

// 引入db 只需要引入，建立连接，保证能在项目开始数据库就与服务器连接
require("./db/index")

var app = express();




// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// 引⼊ swagger
require("./swagger")(app);

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// 路由挂载到路径
app.use('/admins', adminsRouter);
app.use(jwtAuth);
// 路由挂载到路径
app.use('/users', usersRouter);
// 路由挂载到路径
app.use('/', indexRouter);
// 路由挂载到路径
app.use('/schedules', schedulesRouter);
// 将projectionHall路由挂载到/projectionHall路径上
app.use('/projectionHall', projectionHallRouter);
// 挂载电影路由
app.use("/movies", moviesRouter);
// 挂载影院路由
app.use("/cinemas", cinemasRouter);
// 挂载座位路由
app.use("/seat", seatRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
